package com.nave.view;

import nave.control.Options;
import nave.model.Stage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nave.GameActivity;
import com.nave.R;
import com.nave.utils.SoundController;

public class MainMenu extends Activity
{
	public static MediaPlayer mediaPlayer;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Load the sounds */
        SoundController.getReference().initSounds(getApplicationContext());
        mediaPlayer = MediaPlayer.create(this, R.raw.mainsound);
        mediaPlayer.setLooping(true);
        
        setContentView(R.layout.main);
        
        TextView text = (TextView) findViewById(R.id.mainTitle);
        
        text.setTypeface(Typeface.createFromAsset(getAssets(), "SWfont.ttf"));

        buttonSetup();
    }
    @Override
    protected void onResume()
    {
    	super.onResume();

        if ( !mediaPlayer.isPlaying() && Options.sound() ) mediaPlayer.start();
    }
    @Override
    protected void onPause()
    {
    	super.onPause();
    	
    	if ( mediaPlayer.isPlaying() ) mediaPlayer.pause();
    }
    @Override
    protected void onDestroy()
    {
    	super.onDestroy();
    	
    	mediaPlayer.release();
    }

    private void playClickSound()
    {
   		SoundController.getReference().
			playSound(SoundController.CLICK, getApplicationContext());
    }
    
	private void buttonSetup()
	{
		Button startButton = (Button) findViewById(R.id.start);
        Button highscoreButton = (Button) findViewById(R.id.highscores);
        Button optionsButton = (Button) findViewById(R.id.options);
        Button creditsButton = (Button) findViewById(R.id.credits);
        Button quitButton = (Button) findViewById(R.id.quit);
        
        startButton.setOnClickListener(new Button.OnClickListener()
        {        	
			@Override
			public void onClick(View v)
			{
				playClickSound();
				prompt();
			}
		});
        highscoreButton.setOnClickListener(new Button.OnClickListener()
        {	
			@Override
			public void onClick(View v)
			{
				playClickSound();
				MainMenu.this.startActivity(
						new Intent(MainMenu.this, HighScoresActivity.class));
			}
		});
        optionsButton.setOnClickListener(new Button.OnClickListener()
        {	
			@Override
			public void onClick(View v)
			{
				playClickSound();
				MainMenu.this.startActivity(
						new Intent(MainMenu.this, OptionsActivity.class));
			}
		});
        creditsButton.setOnClickListener(new Button.OnClickListener()
        {	
			@Override
			public void onClick(View v)
			{
				playClickSound();
				MainMenu.this.startActivity(
						new Intent(MainMenu.this, CreditsActivity.class));
			}
		});
        quitButton.setOnClickListener(new Button.OnClickListener()
        {
			@Override
			public void onClick(View v)
			{
				playClickSound();
				MainMenu.this.finish();
			}
		});
	}

	private void prompt()
	{
		final Intent it = new Intent(MainMenu.this, GameActivity.class).
			addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

		// TODO restyle the alert box
		AlertDialog.Builder prompt = new AlertDialog.Builder(MainMenu.this);
		prompt.setTitle(R.string.promptTitle);
		prompt.setMessage(R.string.promptMessage);
		prompt.setPositiveButton(R.string.promptCampaign, new OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				playClickSound();
				if ( mediaPlayer.isPlaying() ) mediaPlayer.pause();
				
				it.putExtra("StageType", Stage.STAGES_CAMPAIGN);
//				startActivity(it); // TODO create a campaign mode!
			}
		});
		prompt.setNeutralButton(R.string.promptRandom, new OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				playClickSound();
				if ( mediaPlayer.isPlaying() ) mediaPlayer.pause();

				it.putExtra("StageType", Stage.STAGES_RANDOM);
				startActivity(it);
			}
		});
		prompt.show();
	}
}